



********************************************************************************	
********************************************************************************
************************* MAIN MODEL TABLE FOR THE PAPER ***********************
************************************ FIGURE 5 **********************************
********************************************************************************
********************************************************************************

*NEW - don't need to make a different folder here
*cap mkdir `"$output_fullsample/model_estimation"'

clear all 
eststo clear 

//load the tau estimates for Medicaid 
est use `"$output/model_estimation/parameters/taurf_Medicaid.ster"'
eststo taurf_caid

est use `"$output/model_estimation/parameters/tau_Medicaid_pooledreasons.ster"'
eststo tau_caid_pr

est use `"$output/model_estimation/parameters/tau_Medicaid_byreasons_allgroups.ster"'
eststo tau_caid_byr_ag 

est use `"$output/model_estimation/parameters/tau_Medicaid_byreasons_smallgroups.ster"'
eststo tau_caid_byr_sg 

est use `"$output/model_estimation/parameters/tau_Medicaid_byreasons_largegroups.ster"'
eststo tau_caid_byr_lg

//load the tau estimates for Medicare 
est use `"$output/model_estimation/parameters/taurf_Medicare.ster"'
eststo taurf_care

est use `"$output/model_estimation/parameters/tau_Medicare_pooledreasons.ster"'
eststo tau_care_pr

est use `"$output/model_estimation/parameters/tau_Medicare_byreasons_allgroups.ster"'
eststo tau_care_byr_ag 

est use `"$output/model_estimation/parameters/tau_Medicare_byreasons_smallgroups.ster"'
eststo tau_care_byr_sg 

est use `"$output/model_estimation/parameters/tau_Medicare_byreasons_largegroups.ster"'
eststo tau_care_byr_lg

//load the tau estimates for Commercial 
est use `"$output/model_estimation/parameters/taurf_Commercial.ster"'
eststo taurf_com

est use `"$output/model_estimation/parameters/tau_Commercial_pooledreasons.ster"'
eststo tau_com_pr

est use `"$output/model_estimation/parameters/tau_Commercial_byreasons_allgroups.ster"'
eststo tau_com_byr_ag 

est use `"$output/model_estimation/parameters/tau_Commercial_byreasons_smallgroups.ster"'
eststo tau_com_byr_sg 

est use `"$output/model_estimation/parameters/tau_Commercial_byreasons_largegroups.ster"'
eststo tau_com_byr_lg

//set cd to avoid conflict on BEA server 
cd `"$output\model_estimation"'

		#delimit ;
		esttab taurf_caid tau_caid_pr tau_caid_byr_ag tau_caid_byr_sg tau_caid_byr_lg
			   using "table5_row1_medicaid.tex", replace substitute(_ \_)
		nonumbers not b(3)noobs nonotes nostar
		mtitles("\shortstack{All \\ Phys.}" "\shortstack{All \\ Phys.}" "\shortstack{All \\ Phys.}" "\shortstack{Small \\ Group}" "\shortstack{Large \\ Group}") booktabs 		  coeflabels(_cons "Average $\tau$");
		
		esttab taurf_care tau_care_pr tau_care_byr_ag tau_care_byr_sg tau_care_byr_lg 
			   using "table5_row1_medicare.tex", replace substitute(_ \_)
		nonumbers not b(3) noobs nonotes nostar
		nomtitles booktabs 		  
		coeflabels(_cons "Average $\tau$");
		
		esttab taurf_com tau_com_pr tau_com_byr_ag tau_com_byr_sg tau_com_byr_lg
			   using "table5_row1_commercial.tex", replace substitute(_ \_)
		nonumbers not b(3) noobs nonotes nostar
		nomtitles booktabs 		  
		coeflabels(_cons "Average $\tau$");
		
		#delimit cr

eststo clear 

*NEW
cd "..\..\..\code\main_article\"


//load the CIP estimates for Medicaid 
est use `"$output/model_estimation/parameters/lost_revenue_Medicaid.ster"'
eststo lr_caid

est use `"$output/model_estimation/parameters/CIP_Medicaid_pooledreasons.ster"'
eststo CIP_caid_pr

est use `"$output/model_estimation/parameters/CIP_Medicaid_byreasons_allgroups.ster"'
eststo CIP_caid_byr_ag 

est use `"$output/model_estimation/parameters/CIP_Medicaid_byreasons_smallgroups.ster"'
eststo CIP_caid_byr_sg 

est use `"$output/model_estimation/parameters/CIP_Medicaid_byreasons_largegroups.ster"'
eststo CIP_caid_byr_lg

//load the CIP estimates for Medicare 
est use `"$output/model_estimation/parameters/lost_revenue_Medicare.ster"'
eststo lr_care

est use `"$output/model_estimation/parameters/CIP_Medicare_pooledreasons.ster"'
eststo CIP_care_pr

est use `"$output/model_estimation/parameters/CIP_Medicare_byreasons_allgroups.ster"'
eststo CIP_care_byr_ag 

est use `"$output/model_estimation/parameters/CIP_Medicare_byreasons_smallgroups.ster"'
eststo CIP_care_byr_sg 

est use `"$output/model_estimation/parameters/CIP_Medicare_byreasons_largegroups.ster"'
eststo CIP_care_byr_lg

//load the CIP estimates for Commercial 
est use `"$output/model_estimation/parameters/lost_revenue_Commercial.ster"'
eststo lr_com

est use `"$output/model_estimation/parameters/CIP_Commercial_pooledreasons.ster"'
eststo CIP_com_pr

est use `"$output/model_estimation/parameters/CIP_Commercial_byreasons_allgroups.ster"'
eststo CIP_com_byr_ag 

est use `"$output/model_estimation/parameters/CIP_Commercial_byreasons_smallgroups.ster"'
eststo CIP_com_byr_sg 

est use `"$output/model_estimation/parameters/CIP_Commercial_byreasons_largegroups.ster"'
eststo CIP_com_byr_lg

//set cd to avoid conflict on BEA server 
*NEW
cd `"$output/model_estimation"'

		#delimit ;
		esttab lr_caid CIP_caid_pr CIP_caid_byr_ag CIP_caid_byr_sg CIP_caid_byr_lg
			   using "table5_row2_medicaid.tex", replace substitute(_ \_)
		nonumbers not b(2) nostar noobs nonotes 
		nomtitles keep(_cons) booktabs
		coeflabels(_cons "Average CIP");
		
		esttab lr_care CIP_care_pr CIP_care_byr_ag CIP_care_byr_sg CIP_care_byr_lg 
			   using "table5_row2_medicare.tex", replace substitute(_ \_)
		nonumbers not b(2) nostar noobs nonotes 
		nomtitles keep(_cons) booktabs
		coeflabels(_cons "Average CIP");
		
		
		esttab lr_com CIP_com_pr CIP_com_byr_ag CIP_com_byr_sg CIP_com_byr_lg
			   using "table5_row2_commercial.tex", replace substitute(_ \_)
		nonumbers not b(2) nostar noobs nonotes 
		nomtitles keep(_cons) booktabs
		coeflabels(_cons "Average CIP");
		
		#delimit cr
		
*NEW
cd "..\..\..\code\main_article\"


		
		
		

**********************************************************
** Combine the tables into one tables with three panels **
**********************************************************
cap prog drop panelcombine
prog define panelcombine
qui {
syntax, use(str asis) paneltitles(str asis) columncount(integer) save(str asis) [CLEANup]
preserve

tokenize `"`paneltitles'"'
//read in loop
local num 1
while "``num''"~="" {
local panel`num'title="``num''"
local num=`num'+1
}


tokenize `use'
//read in loop
local num 1
while "``num''"~="" {
tempfile temp`num'
insheet using "``num''", clear
save `temp`num''
local max = `num'
local num=`num'+1
}

//conditional processing loop
local num 1
while "``num''"~="" {
local panellabel : word `num' of `c(alpha)'
use `temp`num'', clear
	if `num'==1 { //process first panel -- clip bottom
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="\end{tabular}" | v1=="}" | v1=="\bottomrule"
	replace v1 = "\midrule \multicolumn{`columncount'}{c}{\textbf{Panel a: `panel1title'}}  \vspace{3pt} \\" if v1=="\midrule" & _n<8

	}
	
	else if `num'==2 { //process middle panels -- clip top and bottom
	//process header to drop everything until first hline
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="}" | v1=="\end{tabular}"
	//replace v1="[1em]" if v1=="\end{tabular}"
	drop if v1=="\midrule"
	drop if v1=="\bottomrule"
	}
	
	else if `num'==3 { //process middle panels -- clip top and bottom
	//process header to drop everything until first hline
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="\end{tabular}" | v1=="}" | v1=="\bottomrule"
	replace v1 = "\multicolumn{`columncount'}{c}{\textbf{Panel b: `panel2title'}}  \vspace{3pt} \\" if v1=="\midrule" & _n<8

	}
		
	else if `num'==4 { //process middle panels -- clip top and bottom
	//process header to drop everything until first hline
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="}" | v1=="\end{tabular}"
	//replace v1="[1em]" if v1=="\end{tabular}"
	drop if v1=="\midrule"
	drop if v1=="\bottomrule"
	}
		
	else if `num'==`max' { //process middle panels -- clip top and bottom
	//process header to drop everything until first hline
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	drop if v1=="\midrule" 
	
	}
		
	else if `num'==5 { //process final panel -- clip top
	//process header to drop everything until first hline
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="\end{tabular}" | v1=="}" | v1=="\bottomrule"
	replace v1 = "\multicolumn{`columncount'}{c}{\textbf{Panel c: `panel3title'}}  \vspace{3pt} \\" if v1=="\midrule" & _n<8

	}
	
	save `temp`num'', replace
local num=`num'+1
}

use `temp1',clear
local num 2
while "``num''"~="" {
append using `temp`num''
local num=`num'+1
}

outsheet using `save', noname replace noquote


	if "`cleanup'"!="" { //erasure loop
	tokenize `use'
	local num 1
		while "``num''"~="" {
		erase "``num''"
		local num=`num'+1
		}
	}

restore
}
end

*NEW
cd `"$output/model_estimation"'
*NEW
panelcombine, use(table5_row1_medicaid.tex table5_row2_medicaid.tex table5_row1_medicare.tex table5_row2_medicare.tex table5_row1_commercial.tex table5_row2_commercial.tex)  columncount(6) paneltitles("Medicaid" "Medicare" "Commercial") save(`"model_main.tex"') cleanup 

*NEW
cd "..\..\..\code\main_article\"

*NEW
copy  "$output/model_estimation/model_main.tex" "$output/paper_inputs/model_main.tex", replace 





